<?php

/*
 * litchicms
 * Copyright (C) ${date} Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

if (!function_exists('Component_ui_datetime')) {

    function Component_ui_datetime($arr=FALSE) {
        !$arr && exit('Component_ui_datetime');

        if (empty($arr['collection_value']) || !is_array($arr['collection_value'])) {
            $arr['collection_value']['addtime'] = $arr['collection_value']['edittime'] = $GLOBALS['BRK_GLOBAL']['datearray'][0];
        } else {
            empty($arr['collection_value']['addtime']) && $arr['collection_value']['addtime'] = $GLOBALS['BRK_GLOBAL']['datearray'][0];
            empty($arr['collection_value']['edittime']) && $arr['collection_value']['edittime'] = $GLOBALS['BRK_GLOBAL']['datearray'][0];
        }

        $input_addtime = input_text_control('addtime', date("Y-m-d H:i:s", $arr['collection_value']['addtime']), 'addtime', "text", "input_text", "120");
        $input_edittime = input_text_control('edittime', date("Y-m-d H:i:s", $GLOBALS['BRK_GLOBAL']['datearray'][0]), 'edittime', "text", "input_text", "120");
        return "
    <div class='content_group_row'>
        <table class='thetable' border='0' cellpadding='0' cellspacing='1'>
            <tr>
                <td class='td1' bgcolor='#ffffff'>
                    <div>
                        <div class='cname'><b>时间控制</b>:</div>
                        <div class='cdesc'>如果要使用系统默认时间,请保留为空</div>
                    </div>
                </td>
                <td class='td2' >
                    <div>
                    添加时间:$input_addtime 最后编辑时间:$input_edittime
                    </div>
                </td>
            </tr>
        </table>
    </div>
    ";
    }

}

$BRK_GLOBAL['ComponentValidationFunction'][] = 'Component_Validation_datetime';

if (!function_exists('Component_Validation_datetime')) {

    function Component_Validation_datetime() {
        if (empty($_POST['field']['addtime'])) {
            $_POST['field']['addtime'] = $GLOBALS['BRK_GLOBAL']['datearray'][0];
        } else {
            $_POST['field']['addtime'] = totimestamp($_POST['field']['addtime']);
            if($_POST['field']['addtime']===FALSE){
                $_POST['field']['addtime']=$GLOBALS['BRK_GLOBAL']['datearray'][0];
            }
        }
        if (empty($_POST['field']['edittime'])) {
            $_POST['field']['edittime'] = $GLOBALS['BRK_GLOBAL']['datearray'][0];
        } else {
            $_POST['field']['edittime'] = totimestamp($_POST['field']['edittime']);
            if($_POST['field']['edittime']===FALSE){
                $_POST['field']['edittime']=$GLOBALS['BRK_GLOBAL']['datearray'][0];
            }
        }
        return TRUE;
    }

}

if (!function_exists('Component_buildUiParam_datetime')) {

    function Component_buildUiParam_datetime($COLLECTION_ID=0, $ONE_DATA=NULL) {
        if (!$COLLECTION_ID || !$ONE_DATA) {
            return array('collection_value' => FALSE);
        }
        return array('collection_value' => array('addtime' => $ONE_DATA['addtime'], 'edittime' => $ONE_DATA['edittime']));
    }

}



if (!function_exists('Component_buildField_datetime')) {

    function Component_buildField_datetime() {
        return array('addtime' => $_POST['field']['addtime'], 'edittime' => $_POST['field']['edittime']);
    }

}